/*
 *  使用ui控件作为打印窗口,为了能在非UI线程中进行UI打印而做的封装
 */
package com.wex.app.tools;

import ohos.agp.components.TextField;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.eventhandler.InnerEvent;

public class UiDisplay extends EventHandler {

    final private int EVENT_SET = 1;
    final private int EVENT_APPEND = 2;

    private TextField text;

    //example: new UiDisplay(EventRunner.getMainEventRunner(), textField);
    public UiDisplay(EventRunner eventRunner, TextField textField) {
        super(eventRunner);
        text = textField;
    }

    @Override
    protected void processEvent(InnerEvent event) {
        switch (event.eventId) {
            case EVENT_SET:
                //转换数据类型,并输出到ui
                if (event.object instanceof String)
                    text.setText((String)(event.object));
                break;
            case EVENT_APPEND:
                //转换数据类型,并输出到ui
                if (event.object instanceof String)
                    text.append((String)(event.object));
                break;
            default:
                break;
        }
    }

    //清空并设置新字符串
    public void set(String s) {
        InnerEvent innerEvent = InnerEvent.get(EVENT_SET, s);
        sendEvent(innerEvent, 0, EventHandler.Priority.IMMEDIATE);
    }

    //追加字符串(不自动换行)
    public void append(String s) {
        InnerEvent innerEvent = InnerEvent.get(EVENT_APPEND, s);
        sendEvent(innerEvent, 0, EventHandler.Priority.IMMEDIATE);
    }
}
